package com.chain.chl.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.SharedElementCallback;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.chain.chl.R;
import com.chain.chl.model.card.Card;
import com.chain.chl.ui.fullimage.FullScreenImageActivity;
import com.chain.chl.util.FrescoLoadUtils;
import com.chain.chl.util.Photo;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class ImageMainActivity extends AppCompatActivity {


	private static String INTENT_CARD = "card";

	private int mEnterPosition;
	private int mExitPosition;

	@BindView(R.id.recycler_view)
	RecyclerView recyclerView;


	public static void open(Context context,Card card){
		Intent intent = new Intent(context,ImageMainActivity.class);
		intent.putExtra(INTENT_CARD, (Serializable) card);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_imagemain);
		ButterKnife.bind(this);

		recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
		recyclerView.setAdapter(new RecyclerView.Adapter() {
			@Override
			public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
				ImageView imageView = new ImageView(getApplicationContext());
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				return new ItemViewHolder(imageView);
			}

			@Override
			public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
				final ImageView imageView = (ImageView) holder.itemView;
				int screenWidth = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
				int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getApplicationContext().getResources().getDisplayMetrics());
				imageView.setPadding(padding, padding, padding, padding);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(screenWidth / 3, screenWidth / 3));
				imageView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.white));
				Glide.with(ImageMainActivity.this).load(Photo.PHOTO_LIST[position]).into(imageView);

				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					imageView.setTransitionName(String.valueOf(position));
				}
				imageView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent intent = new Intent(ImageMainActivity.this, FullScreenImageActivity.class);
						intent.putExtra(FullScreenImageActivity.EXTRA_DEFAULT_INDEX, holder.getAdapterPosition());
						mEnterPosition = mExitPosition = holder.getAdapterPosition();
						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
							startActivity(intent, ActivityOptionsCompat.makeSceneTransitionAnimation(ImageMainActivity.this, imageView, imageView.getTransitionName()).toBundle());
						}
					}
				});
			}

			@Override
			public int getItemCount() {
				return Photo.PHOTO_LIST.length;
			}
		});

		setExitSharedElementCallback(new SharedElementCallback() {
			@Override
			public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
				if (mExitPosition != mEnterPosition) {
					names.clear();
					sharedElements.clear();
					names.add(String.valueOf(mExitPosition));
					sharedElements.put(String.valueOf(mExitPosition), recyclerView.findViewHolderForAdapterPosition(mExitPosition).itemView);
				}
			}
		});

	}

	@Override
	public void onActivityReenter(int resultCode, Intent data) {
		if (resultCode == RESULT_OK && data != null) {
			mExitPosition = data.getIntExtra(FullScreenImageActivity.EXTRA_EXIT_INDEX, -1);
		}
	}

	class ItemViewHolder extends RecyclerView.ViewHolder {

		ItemViewHolder(View itemView) {
			super(itemView);
		}
	}
}
